//
//  DGAccountCredential.m
//  DGAccountKit
//
//  Created by Jinxiao on 11/19/13.
//  Copyright (c) 2013 debugeek. All rights reserved.
//

#import "DGAccountCredential.h"
#import "DGKeychain.h"

@implementation DGAccountCredential

- (BOOL)validateCredentials
{
    return (self.userID.length > 0 && self.token.length > 0);
}

- (BOOL)loadCredentials
{
    if(self.userID.length == 0)
    {
        return NO;
    }
    
    NSString *serviceName = [NSString stringWithFormat:@"DGAccountCredential_%@", self.userID];
    
    if(self.token.length == 0)
    {
        self.token = [DGKeychain queryPasswordForUsername:kKeyChainToken serviceName:serviceName error:nil];
    }
    
    return YES;
}

- (BOOL)saveCredentials
{
    if(![self validateCredentials])
    {
        return NO;
    }
    
    NSString *serviceName = [NSString stringWithFormat:@"DGAccountCredential_%@", self.userID];
    
    if(self.token.length > 0)
    {
        [DGKeychain storeUsername:kKeyChainToken password:self.token serviceName:serviceName updateExisting:YES error:nil];
    }
    
    return YES;
}

@end